package com.example.controller;

import com.example.util.Constants;
import com.example.util.JpushUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2017/10/3.
 *
 * 极光推送
 */
@RestController
@RequestMapping("api/jpush")
public class JpushController {

    /**
     * 日志记录器
     */
    private static Logger logger = Logger.getLogger(JpushController.class);


    private static String appkey = "e129689e1c8e12edebf777c0";

    private static String masterSecret = "90c4fdb67e54917766fec16a";


    @ApiOperation(value = "极光推送",notes = "极光推送")
    @RequestMapping(value="/push",method = RequestMethod.POST)
    public Map<String, Object> push(@RequestBody Map<String, Object> inMap) {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        logger.info("############### 进入，数据为：" + inMap.toString());


        String title = (String) inMap.get("title");
        String message = (String) inMap.get("message");
        String platform = (String) inMap.get("platform");

        boolean isProductEnv = true;
        // ios 环境
        String isProductEnvStr = (String) inMap.get("isProductEnv");
        if (StringUtils.isNotEmpty(isProductEnvStr) && !"1".equals(isProductEnvStr)) {
            isProductEnv = false;
        }

        JpushUtil js = new JpushUtil(appkey, masterSecret, message, title);

        boolean isSendSuccess = false;
        // 增加了一个参数deviceIds
        if (StringUtils.isEmpty(platform)) {
            List<String> deviceIds = (List) inMap.get("DeviceIds");
            isSendSuccess = js.sendPushAll(isProductEnv, deviceIds);
        } else {
            // 发送给ios用户
            if (Constants.PLATFORM_IOS.equals(platform)) {
                List<String> deviceIds = (List) inMap.get("iosDeviceIds");
                String[] deviceArr = listToArr(deviceIds);

                if (null != deviceArr && deviceArr.length > 0) {
                    isSendSuccess = js.sendPushIosDevices(isProductEnv, deviceArr);
                    logger.info("############### 推送 ios：" + StringUtils.join(deviceArr,","));
                } else {
                    isSendSuccess = js.sendPushIosAll(isProductEnv);
                }

            } else if (Constants.PLATFORM_ANDROID.equals(platform)) {
                List<String> deviceIds = (List) inMap.get("AndrDeviceIds");
                String[] deviceArr = listToArr(deviceIds);
                logger.info("############### 推送 android：" + StringUtils.join(deviceArr,","));
                isSendSuccess = js.sendPushAndroidDevices(deviceArr);

            } else if (Constants.PLATFORM_ANDROID_AND_IOS.equals(platform)) {
                // 推送android消息
                List<String> deviceIds = (List) inMap.get("AndrDeviceIds");
                String[] deviceArr = listToArr(deviceIds);
                logger.info("############### 推送 android：" + StringUtils.join(deviceArr,","));
                if (null != deviceArr && deviceArr.length > 0) {
                    isSendSuccess = js.sendPushAndroidDevices(deviceArr);
                }

                // 发送IOS消息
                List<String> deviceIds2 = (List) inMap.get("iosDeviceIds");
                String[] deviceArr2 = listToArr(deviceIds2);

                if (null != deviceArr2 && deviceArr2.length > 0) {
                    isSendSuccess = js.sendPushIosDevices(isProductEnv, deviceArr2);
                    logger.info("############### 推送 ios：" + StringUtils.join(deviceArr,","));
                }
            }
        }

        String isSuccess = "0";
        String msgInf = "推送失败";
        if (isSendSuccess) {
            isSuccess = "1";
            msgInf = "推送成功";
            logger.info("############### 推送成功!");
        } else {
            logger.info("############### 推送失败!");
        }
        resultMap.put("isSuccess", isSuccess);
        resultMap.put("msgInf", msgInf);
        String json = JSONObject.valueToString(resultMap);

        if (logger.isInfoEnabled()) {
            logger.info("给客户端的数据: " + json);
        }
        return resultMap;
    }

    /**
     * 把list转换成数组
     *
     * @param list
     * @return
     */
    public String[] listToArr(List<String> list) {
        String[] arr = null;
        if (null != list && !list.isEmpty()) {
            arr = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                arr[i] = list.get(i);
            }
        }
        return arr;
    }

}
